package android.ijiajiao.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.http.Part;

import android.ijiajiao.dao.UserDao;
import android.ijiajiao.util.FileSavePathUtil;
import android.ijiajiao.util.JDBCConnectUtil;
import android.ijiajiao.util.SaveImgUtil;
import android.ijiajiao.util.SendEmailUtil;
import net.sf.json.JSONObject;

public class UserDaoImpl implements UserDao {

	/**
	 * 判断用户名是否可用
	 */
	@Override
	public boolean RegisterName(String name) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select * from user where name=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			rs = ps.executeQuery();
			while (rs.next()) {
				flag = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			JDBCConnectUtil.close(rs, null, ps, conn);
		}

		return flag;
	}

	/**
	 * 判断email是否可用
	 */
	@Override
	public boolean RegisterEmail(String email) {
		boolean flag = false;
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select * from user where email=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, email);
			rs = ps.executeQuery();
			while (rs.next()) {
				flag = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			JDBCConnectUtil.close(rs, null, ps, conn);
		}

		return flag;
	}

	/**
	 * 登陆
	 */
	@Override
	public JSONObject Login(String email, String pass) {
		// TODO Auto-generated method stub
		JSONObject data = null;
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select name,status from user where email=? and pass=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, email);
			ps.setString(2, pass);
			rs = ps.executeQuery();

			while (rs.next()) {
				data = new JSONObject();
				data.put("name", rs.getString(1));
				data.put("status", rs.getInt(2));
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			JDBCConnectUtil.close(rs, null, ps, conn);
		}
		return data;
	}

	/**
	 * 注册
	 */
	@Override
	public boolean Register(String name, String email, String pass, String status) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "insert into user (name,email,pass,status) values (?,?,?,?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, email);
			ps.setString(3, pass);
			ps.setString(4, status);
			int row = ps.executeUpdate();
			if (row > 0) {
				String sql1 = "insert into identi (name) values(?)";
				if (ps != null) {
					ps.close();
				}
				ps = conn.prepareStatement(sql1);
				ps.setString(1, name);
				int row1 = ps.executeUpdate();
				if (row1 > 0) {
					flag = true;
				}
			}

		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			JDBCConnectUtil.close(null, null, ps, conn);
		}
		return flag;
	}

	/*
	 * 找回密码 (non-Javadoc)
	 * 
	 * @see android.ijiajiao.dao.UserDao#FindPass(java.lang.String)
	 */
	@Override
	public boolean FindPass(String email) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String name = null, pass = null;
		String sql = "select name,pass from user where email=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, email);
			rs = ps.executeQuery();
			while (rs.next()) {
				name = rs.getString(1);
				pass = rs.getString(2);
			}
			flag = new SendEmailUtil().SendEmail(email, name, pass);

		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			JDBCConnectUtil.close(rs, null, ps, conn);
		}

		return flag;
	}

	/**
	 * 成功返回名字和图片地址
	 */
	@Override
	public ArrayList<Object> motifyUserImg(String name, Part part, String path) {
		// TODO Auto-generated method stub
		ArrayList<Object> data = new ArrayList<>();
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "update user set img=? where name=?";
		try {
			String savePath = FileSavePathUtil.USER_IMG;
			String imgurl = new SaveImgUtil().saveImg(path, part, savePath);
			if (imgurl != null) {
				ps = conn.prepareStatement(sql);
				ps.setString(1, imgurl);
				ps.setString(2, name);
				int row = ps.executeUpdate();
				if (row > 0) {
					data.add(name);
					data.add(imgurl);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
		} finally {
			JDBCConnectUtil.close(null, null, ps, conn);
		}

		return data;
	}

	/**
	 * 修改用户资料
	 */

	@Override
	public ArrayList<Object> motifyUserData(String name, String sex, int age, String phone, String qq, String adr) {
		// TODO Auto-generated method stub
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "update user set sex=?,age=?,phone=?,qq=?,adr=? where name=?";
		ArrayList<Object> data = new ArrayList<>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, sex);
			ps.setInt(2, age);
			ps.setString(3, phone);
			ps.setString(4, qq);
			ps.setString(5, adr);
			ps.setString(6, name);
			int row = ps.executeUpdate();
			if (row > 0) {
				data.add(name);
				data.add(sex);
				data.add(age);
				data.add(phone);
				data.add(qq);
				data.add(adr);
			}
		} catch (Exception e) {
			System.out.println(e);
		} finally {

			JDBCConnectUtil.close(null, null, ps, conn);
		}
		return data;
	}

	/**
	 * 修改密码 pass为新密码 passed为旧密码
	 */
	@Override
	public boolean motifyPass(String name, String pass, String passed) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "update user set pass=? where name=?,pass=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, pass);
			ps.setString(2, name);
			ps.setString(3, passed);
			int row = ps.executeUpdate();
			if (row > 0) {
				flag = true;
			}
		} catch (Exception e) {
			System.out.println(e);
		} finally {
			JDBCConnectUtil.close(null, null, ps, conn);
		}

		return flag;
	}

	/**
	 * 用户资料
	 */

	@Override
	public ArrayList<Object> userData(String name) {
		// TODO Auto-generated method stub
		ArrayList<Object> data = new ArrayList<>();
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select sex,age,phone,qq,adr from user where name=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			rs = ps.executeQuery();
			while (rs.next()) {
				data.add(name);
				data.add(rs.getString(1));
				data.add(rs.getString(2));
				data.add(rs.getString(3));
				data.add(rs.getString(4));
				data.add(rs.getString(5));
			}

		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			JDBCConnectUtil.close(rs, null, ps, conn);
		}

		return data;
	}
}
